<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-date-picker
        v-model="dateInput"
        class="filter-item"
        type="datetimerange"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :default-time="['00:00:00', '23:59:59']"
        style="width: 260px"
        @change="dateInputHandle"
      />
      <el-select
        v-model="type"
        placeholder="请选择"
        class="filter-item"
        style="margin-left: 10px; width: 100px"
      >
        <el-option label="账号" :value="0" />
        <el-option label="姓名" :value="1" />
      </el-select>
      <el-input
        v-if="type == 0"
        v-model="listQuery.user"
        placeholder="请输入账号"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-if="type == 1"
        v-model="listQuery.name"
        placeholder="请输入姓名"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="请选择状态"
        class="filter-item"
        style="margin-left: 10px; width: 150px"
        @change="selectHandle"
      >
        <el-option label="全部状态" value="" />
        <el-option label="审核中" :value="0" />
        <el-option label="同意" :value="1" />
        <el-option label="拒绝" :value="2" />
        <el-option label="无效" :value="3" />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        icon="iconfont qt-caozuo-xunhuan1"
        @click="clearFilter"
      >
        重置
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="账号" prop="user.user" align="center" />
      <el-table-column label="姓名" align="center" prop="user.name">
        <template slot-scope="{ row }">
          {{ row.user && row.user.name }}
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center">
        <template slot-scope="{ row }">
          <div>
            {{ row.type | typeFilter }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source">
        <template slot-scope="{ row }">
          {{ row.source | sourceFilter }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          <div>
            {{ row.status | statusFilter }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="created_at" />
      <el-table-column label="变动金额" align="center">
        <template slot-scope="{ row }">
          {{ row.expend == "1" ? "+" : "-" }}{{ row.money }}
        </template>
      </el-table-column>
      <el-table-column label="操作后金额" align="center" prop="balance_money" />
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getWalletCashOut, updateWalletCashOut } from '@/api/common'
import { utcAdd } from '@/utils'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'FinanceWalletCashOut',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  filters: {
    typeFilter(val) {
      let result = ''
      switch (val) {
        case 0:
          result = '支付'
          break
        case 1:
          result = '充值'
          break
        case 2:
          result = '提现'
          break
        case 3:
          result = '结算'
          break
      }
      return result
    },
    statusFilter(val) {
      let result = ''
      switch (val) {
        case 0:
          result = '审核中'
          break
        case 1:
          result = '同意'
          break
        case 2:
          result = '拒绝'
          break
        case 3:
          result = '无效'
          break
      }
      return result
    },
    //   1充值-微信，2充值-后台添加，3充值-支付宝，4结算-店铺，5结算-佣金
    sourceFilter(val) {
      let result = ''
      switch (val) {
        case 1:
          result = '充值-微信'
          break
        case 2:
          result = '充值-后台添加'
          break
        case 3:
          result = '充值-支付宝'
          break
        case 4:
          result = '结算-店铺'
          break
        case 5:
          result = '结算-佣金'
          break
      }
      return result
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      type: 0,
      dateInput: undefined,
      listQuery: {
        page: 1,
        per_page: 20,
        start_time: undefined,
        end_time: undefined,
        name: undefined,
        user: undefined,
        status: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取时间
    dateInputHandle(e) {
      if (e && e.length > 0) {
        this.listQuery.start_time = utcAdd(e[0])
        this.listQuery.end_time = utcAdd(e[1])
      } else {
        this.listQuery.start_time = undefined
        this.listQuery.end_time = undefined
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.listQuery['start_time'] = undefined
      this.listQuery['end_time'] = undefined
      this.listQuery['name'] = undefined
      this.listQuery['user'] = undefined
      this.listQuery['status'] = ''
      this.dateInput = undefined
      this.handleFilter()
    },
    selectHandle(e) {
      if (e == 1) {
        this.listQuery['user'] = undefined
      } else {
        this.listQuery['name'] = undefined
      }
    },
    getList() {
      this.listLoading = true
      getWalletCashOut(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
